<?php
namespace app\wechat\controller;
use think\Controller;
use think\Request;
use think\Hook;
use Wechat\TPWechat;

class Common extends Controller
{
    protected $_action;
    protected $_controller;
    protected $wechatObj;

    public function _initialize() {
        //dump(session(''));exit;
        $request = request();
        $this->_action = $request->action();
        $this->_controller = $request->controller();
        if (session('login') != 1 || !session('?member_id')) {
            $this->redirect('login/index');
        }
    }

    protected function getWechtObj() {
        if ($this->wechatObj == null) {
            $options = config('wechat');
            $this->wechatObj = new TPWechat($options);
        }
        return $this->wechatObj;
    }

    function is_weixin() {
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
        }
        return false;
    }

    //微信回调页面
    public function baseweixinlogin() {
        $code = isset($_GET['code']) ? $_GET['code'] : '';
        if (!$code)
            $this->error('错误页面1');

        $oauthAccessToken = $this->getWechtObj()->getOauthAccessToken();
        //dump($oauthAccessToken);

        if (!$oauthAccessToken)
            $this->error('获取授权失败');

        $userInfo = $this->getWechtObj()->getUserinfo($oauthAccessToken['openid']);
        //dump($userInfo);exit;
        if (isset($userInfo['subscribe']) && $userInfo['subscribe'] == 0) {
            $data = session('back_weixin_path_keys');
            if (isset($data['action']) && $data['action'] == 'distribution_') {
                $scan_log_data = [
                    'p_id' => $data['id'],
                    'p_phone' => $data['phone'],
                    'openid' => $oauthAccessToken['openid'],
                    'action' => $data['action']
                ];
                db('scan_log')->insert($scan_log_data);
            }

            if (isset($data['action']) && $data['action'] == 'gathering_') {
                $scan_log_data = [
                    'repair_id' => $data['repair_id'],
                    'openid' => $oauthAccessToken['openid'],
                    'action' => $data['action']
                ];
                db('scan_log')->insert($scan_log_data);
            }
            //if ()
            $this->redirect(url('base/subscribe'));
        }
//        dump($userInfo);
//        exit;
        if (!$userInfo){
            unlink('./access_token.json'); // 删除access_token 文件
            $url = 'http://' . $_SERVER['HTTP_HOST'] . url('Common/baseweixinlogin');
            $url = $this->getWechtObj()->getOauthRedirect($url, '', 'snsapi_base');
            exit('<script>window.location.href="'.$url.'"</script>');
        }

        session('openid', $userInfo['openid']);
        session('nickname', $userInfo['nickname']);
        session('headimgurl', $userInfo['headimgurl']);

        $map['openid'] = session('openid');
        $map['band_status'] = 1;
        $member = db('member')->field('id, phone')->where($map)->find();

        session('member_id', $member['id']);
        session('phone', $member['phone']);

//        dump($member);
//        dump(session(''));
//        exit;
        if (!$member && session('back_weixin_path') != '/wechat/w_distribution/register') {
            $this->redirect(url('login/index'));
        }
        //exit;
        $this->redirect(session('back_weixin_path'),session('back_weixin_path_keys'));
    }
}
